import Mock from 'mockjs'
import { PublicConfiguration_Limit } from '@utils/PublicConfiguration'
import Components from './Components'

/**
 * @date: 2024-10-06 20:01:58
 * @description: 用于存储商品表格数据
 * @modifyContent:
 * @author: Forever丿顾北
**/
let CommodityTableList = []

/**
 * @date: 2024-10-06 20:14:39
 * @description: 获取商品表格数据
 * @modifyContent:
 * @author: Forever丿顾北
**/

const getCommodityTableData = (config) => {
  // 如果商品列表为空，则生成数据
  if (CommodityTableList.length === 0) {
    for (let i = 0; i < Components.count; i++) {
      const brandName = () => Mock.Random.pick(['Apple', 'Samsung', 'Xiaomi', 'Huawei', 'OnePlus'])
      const brandInitials = () => (brandName()).charAt(0).toUpperCase()
      const productNamePick = () => {
        const adjectives = Mock.Random.pick(['优质', '高效', '时尚', '耐用', '智能'])
        const nouns = Mock.Random.pick(['手机', '相机', '耳机', '电脑', '手表'])
        const verbs = Mock.Random.pick(['拍摄', '通话', '连接', '办公', '运动'])
        return { adjectives, nouns, verbs }
      }

      const StringName1 = brandName()
      const StringName2 = productNamePick().adjectives
      const StringName3 = productNamePick().verbs
      const StringName4 = productNamePick().nouns
      const StringName5 = brandInitials()
      const productNamePickRetuen = `${StringName1}-${StringName2}${StringName3}${StringName4}${StringName5}`
      const productDesc = `${StringName1}的${StringName2}${StringName3}${StringName4}${StringName5}，让您的生活更加便捷。`
      CommodityTableList.push(
        Mock.mock({
          id: Mock.Random.guid(),
          productName: productNamePickRetuen,
          productNo: Mock.Random.guid(),
          productPrice: Mock.Random.float(100, 10000, 2, 2),
          productNum: Mock.Random.integer(1, 100),
          productType: StringName4,
          productDesc,
          productImg: [Mock.Random.image('180x100', Mock.Random.color())]
        })
      )
    }
  }

  // 解析 URL 参数
  const { productName, page = 1, limit = PublicConfiguration_Limit.limit } = Components.Param2Obj(config.url)

  // 过滤商品列表
  const MockList = CommodityTableList.filter(item => {
    return !productName || (item.productName.includes(productName))
  })
  // 分页商品列表
  let PageMockList = []
  PageMockList = MockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
  // 返回分页数据和总记录数
  return {
    code: 200,
    msg: '获取成功',
    data: {
      tableData: PageMockList,
      total: MockList.length
    }
  }
}

/**
 * @date: 2024-10-06 20:47:52
 * @description:新增商品表格数据
 * @author: Forever丿顾北
 * @param { String | productName } 商品名称
 * @param { Float | productPrice } 商品单价
 * @param { Number | productNum } 商品库存
 * @param { String | productType } 商品类型
 * @param { String | productDesc } 商品描述
 * @param { Array | productImg } 商品图片
**/
const AddCommodityTableData = (config) => {
  const { productName, productPrice, productNum, productType, productDesc } = JSON.parse(config.body)
  CommodityTableList.unshift({
    id: Mock.Random.guid(),
    productName,
    productNo: Mock.Random.guid(),
    productPrice,
    productNum,
    productType,
    productDesc,
    productImg: [Mock.Random.image('200x100', Mock.Random.color())]
  })
  return {
    code: 200,
    msg: '新增成功',
    data: {}
  }
}

/**
 * @date: 2024-10-06 20:52:53
 * @description:编辑商品表格数据
 * @author: Forever丿顾北
 * @param { String | id } id
 * @param { String | productName } 商品名称
 * @param { Float | productPrice } 商品单价
 * @param { Number | productNum } 商品库存
 * @param { String | productType } 商品类型
 * @param { String | productDesc } 商品描述
 * @param { Array | productImg } 商品图片
**/
const EditCommodityTableData = (config) => {
  const { id, productName, productPrice, productNum, productType, productDesc } = JSON.parse(config.body)
  CommodityTableList.some(item => {
    if (item.id === id) {
      item.productName = productName
      item.productNo = Mock.Random.guid()
      item.productPrice = productPrice
      item.productNum = productNum
      item.productType = productType
      item.productDesc = productDesc
      item.productImg = [Mock.Random.image('200x100', Mock.Random.color())]
      return true
    }
    return true
  })
  return {
    code: 200,
    msg: '编辑成功',
    data: {}
  }
}

/**
 * @date: 2024-10-06 20:55:03
 * @description:删除商品表格数据
 * @author: Forever丿顾北
 * @param { String | id } id
**/
const deleteCommodityTableData = (config) => {
  const { id } = JSON.parse(config.body)
  if (!id) {
    return {
      code: -999,
      msg: '参数不正确',
      data: {}
    }
  } else {
    CommodityTableList = CommodityTableList.filter(item => item.id !== id)
    return {
      code: 200,
      msg: '删除成功',
      data: {}
    }
  }
}
const Commodity = {
  getCommodityTableData, // 获取商品表格数据
  AddCommodityTableData, // 新增商品表格数据
  EditCommodityTableData, // 编辑商品表格数据
  deleteCommodityTableData // 删除商品表格数据
}

export default Commodity
